#!/usr/bin/env ruby

def avg(data)
  return data.inject { |x,y| x + y }.to_f / data.length.to_f
end


results = {}
# results[size][granted|denied] = [measurements, ...]

# Cycle through sizes 1,5,10, ..., 100

policyName = "policySize"

([1] + (1..20).map { |i| i * 5 }).each do |size|
  results[size] = {:granted => [], :denied => []}
  # Take all 10 policies for the size
  nr = 0
  10.times do
    # Each policy has {size} random allowed usages
    policyName = "policySize#{size}Nr#{nr}.n3"
    
    # let's go through the 10 requests
    (0..9).each do |reqnr|
      reqName = "request#{reqnr}"
      result, time = `../matcher/run.sh ../background/materialized.n3 ../datagen/testdata/#{policyName} ../datagen/testdata/#{reqName}.n3 http://request.com/\##{reqName} p`.split(/ /)
      time = time.to_f  
      if result =~ /granted/
      	 results[size][:granted] << time
      elsif result =~ /denied/
      	 results[size][:denied] << time
      else  
        puts "Invalid result: #{result}"
      end  
    end   
    nr += 1
  end
end

open("results.txt","w") do |out|
  out.puts "size\tgranted\tdenied\tall"
  results.keys.sort.each do |size|
    out.puts "#{size}\t#{avg(results[size][:granted])}\t#{avg(results[size][:denied])}\t#{avg(results[size][:granted] + results[size][:denied])}"
  end
end
